package com.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.dto.StateDTO;
import com.domain.po.Product;
import com.enums.ProductStart;
import com.exception.DbException;
import com.mapper.ProductMapper;
import com.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component

    public class StateTask {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductMapper productMapper;

        //让商品上线的方法
        @Scheduled(cron = "0 0 0 * * ? ")//每天上午00点触发一次
        public void timeOnTask(){
            log.info("每天定时让商品上线", LocalDateTime.now());
            //select * from product where product_state = ? and time_on >= 当前时间
            QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>()
                    .eq("product_state", ProductStart.AGREE_CHECK)
                    .ge("time_on", LocalDateTime.now()); // 查询时间上线时间大于等于当前时间的商品
            // 执行查询，得到需要上线的商品列表
            List<Product> productsToOnline = productMapper.selectList(queryWrapper);

            // 遍历商品列表，执行上线操作
            for (Product product : productsToOnline) {
                // 声明StateDTO类
                StateDTO stateDTO = new StateDTO();
                stateDTO.setProductId(product.getProductId());
                stateDTO.setProductState(ProductStart.ON_LINE.getValue());
                try {
                    // 调用服务层方法来更新商品状态
                    productService.updateState(stateDTO);
                    log.info("商品{}已上线", product.getProductId());
                } catch (Exception e) {
                    throw new DbException("上线失败");
                }
            }
        }
        //让商品下线的方法
        @Scheduled(cron = "0 0 0 * * ? ")
        public void timeOffTask() {
            log.info("每天定时让商品下线", LocalDateTime.now());
            //select * from product where product_state = ? and time_off <= 当前时间
            QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>()
                .eq("product_state", ProductStart.ON_LINE)
                .le("time_on", LocalDateTime.now()); // 查询时间上线时间大于等于当前时间的商品
            // 执行查询，得到需要下线的商品列表
            List<Product> productsToOffline = productMapper.selectList(queryWrapper);

            // 遍历商品列表，执行下线操作
            for (Product product : productsToOffline) {
            // 声明StateDTO类
            StateDTO stateDTO = new StateDTO();
            stateDTO.setProductId(product.getProductId());
            stateDTO.setProductState(ProductStart.OFF_LINT.getValue());
            try {
                // 调用服务层方法来更新商品状态
                productService.updateState(stateDTO);
                log.info("商品{}已下线", product.getProductId());
            } catch (Exception e) {
                throw new DbException("下线失败");
            }
        }
    }
}

